/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lottery;


import java.util.Random;
/**
 *
 * @author Matrix
 */
class Lottery {

    private static final int[] rangeArr = {621920,0,190000,2,100000,4,40000,6,30000,8,7500,10,6000,12,3000,20,1000,40,300,50,200,60,50,100,12,500,5,1000,5,2000,4,5000,2,10000,1,50000,1,100000};

    private static final int size = 9;

    private static final int minSymbolsToWin = 4;

    private int[] winArr;

    public Lottery() {
        if (isValidClass() == true) winArr = ACl.shuffle(genWinArr(rangeArr));
        else System.out.println("Invalid data");
    }

    public int[] getWinArr() {
        return winArr;
    }

    public int getWinArrLength() {
        return winArr.length;
    }

    private int getWin() {
        int ticket = winArr[new Random().nextInt(winArr.length)];
        //System.out.println(ticket);
        return ticket;
    }

    private int[] genSalt() {
        // minSymbolsToWin < size;
        int[] salt = new int[(rangeArr.length/2-1)*(minSymbolsToWin-1)];
        int j = 3;
        for (int i = 0; i < salt.length; i++) {
            if ((i>0)&&((i % (minSymbolsToWin-1)) == 0)) j += 2;
            salt[i] = rangeArr[j];
            }
        salt = ACl.shuffle(salt);
        return salt;
    }

    public int[] getFullTicket() {
        // minSymbolsToWin < size;
        int[] fullTicket = new int[size];
        int win = getWin();
        //System.out.println("win: " + win);
        int[] salt = genSalt();
        //System.out.println("SaltArr: ");
        //ACl.prnArray(salt);
        for (int i = 0; i < size; i++) {
            if ((win != 0) && (i < minSymbolsToWin)) {
                fullTicket[i] = win;
            }
            else {
                // TODO fullTicket[i] = 0;
                fullTicket[i] = salt[i];
            }
        }
        ACl.shuffle(fullTicket);
        return fullTicket;
    }

    public static boolean isValidClass() {
        if (((rangeArr.length % 2) == 0) && (size >= minSymbolsToWin) && (size<(rangeArr.length/2*(minSymbolsToWin-1)))) {
            return true;
        }
        else return false;
    }

    public static int[] genWinArr(int[] rangeArr) {
        if ((rangeArr.length % 2) == 0) {
            int winArrLength = 0, allWinsCount = 0;
            for (int i = 0; i < rangeArr.length; i++) {
                winArrLength += rangeArr[i];
                allWinsCount += rangeArr[i]*rangeArr[i+1];
                i++;
            }
            //System.out.println(allWinsCount);
            int[] winArr = new int[winArrLength];
            int j = 0, k = 0;
            for (int i = 0; i < rangeArr.length; i++) {
                j = 0;
                while (j < rangeArr[i]) {
                    winArr[k] = rangeArr[i+1];
                    j += 1;
                    k += 1;
                }
                i += 1;
            }
            return winArr;
        }
        else {int[] x = {1,2,3}; return x;}
    }

    public double testThisPercent(int cost) {
        double countWins = 0;
        for (int i = 0; i < (winArr.length*4); i++) {
            countWins += getWin();
        }
        return countWins/(winArr.length*cost*4);
    }
}
